package com.lanou.tipsy.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.lanou.tipsy.R;
import com.lanou.tipsy.base.BaseActivity;
import com.lanou.tipsy.tools.myConfig.MyConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.exception.SocializeException;

import java.util.Map;
import java.util.Set;

/**
 *
 *   登陆界面 含有VideoView  播放本地资源文件
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    public static final String TAG = "LoginActivity";
    private Uri mUri;
    //用于播放资源文件
    private VideoView video_view;
    //sina,QQ登陆按钮
    private ImageView sinaLogin,tencentLogin;
    UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");
    private String userIcon,userName;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //读取本地视频资源文件
        mUri = Uri.parse( "android.resource://" + getPackageName() + "/" +R.raw.login_video );
        video_view = (VideoView) findViewById(R.id.video_view);
        // Create media controller，组件可以控制视频的播放，暂停，回复，seek等操作，不需要你实现
        MediaController mMediaController = new MediaController(this);
        video_view.setMediaController(mMediaController);
        //新浪客户端自动登录
        //mController.getConfig().setSsoHandler(new SinaSsoHandler());
        sinaLogin = (ImageView) findViewById(R.id.sinaLogin);
        tencentLogin = (ImageView) findViewById(R.id.tencentLogin);
        sinaLogin.setOnClickListener(this);
        tencentLogin.setOnClickListener(this);
    }
    //播放视频
    public void onStart() {
        video_view.setVideoURI(mUri);
        video_view.start();

        super.onStart();
    }

    //登录按钮监听
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sinaLogin:
                mController.doOauthVerify(LoginActivity.this, SHARE_MEDIA.SINA, new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        Toast.makeText(LoginActivity.this,"授权开始",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete(Bundle bundle, SHARE_MEDIA share_media) {
                        Toast.makeText(LoginActivity.this,"授权完成",Toast.LENGTH_SHORT).show();
                        //获取相关授权信息
                        mController.getPlatformInfo(LoginActivity.this,SHARE_MEDIA.SINA, new SocializeListeners.UMDataListener() {
                            @Override
                            public void onStart() {
                                Toast.makeText(LoginActivity.this, "开始获取平台数据", Toast.LENGTH_SHORT).show();
                            }
                            @Override
                            public void onComplete(int status, Map<String, Object> info) {
                                Log.i("TESTDATA","这里status为:"+status);
                                if (status == 200 && info != null){
                                    StringBuilder stringBuilder = new StringBuilder();
                                    Set<String> keys = info.keySet();
                                    for (String key : keys){
                                        stringBuilder.append(key + "=" + info.get(key).toString() + "\r\n");
                                    }
                                    String icon = info.get("profile_image_url").toString();
                                    MyConfig.putSharePre(LoginActivity.this,"user","userIcon",icon);

                                    String name = info.get("screen_name").toString();
                                    MyConfig.putSharePre(LoginActivity.this,"users","userName",name);

                                    userIcon = MyConfig.getSharePreStr(LoginActivity.this, "users", "userIcon");
                                    userName = MyConfig.getSharePreStr(LoginActivity.this,"user","userName");

                                    if (userIcon != null && userName != null && userIcon != "0" && userName != "0"){
                                        new MyThread().start();
                                    }
                                    /**
                                     * 获取,并返回用户昵称,头像的URL
                                     */
                                    Log.d("TESTDATA", stringBuilder.toString());
                                    //String icon = info.get("profile_image_url").toString();
                                    //String name = info.get("screen_name").toString();
                                    Intent intent = new Intent();
                                    intent.putExtra("profile_image_url",icon);
                                    intent.putExtra("screen_name",name);
                                    setResult(100,intent);
                                    finish();
                                } else {
                                    Log.d("TESTDATA","发生错误" + status);
                                }
                            }
                        });
                    }

                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA share_media) {
                        Toast.makeText(LoginActivity.this,"授权错误",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
                        Toast.makeText(LoginActivity.this,"授权取消",Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.tencentLogin:
               Toast.makeText(LoginActivity.this, "QQ服务器宕了,别问我为什么,这事儿得找老马", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    public class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            try {
                sleep(3000);
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}